package algebra.visitors;

import java.util.List;



import kb.DescriptionLogicsKB;

import utils.parser.Parser;
import utils.exception.SyntaxParsingException;

import algebra.ProjectionDescription;
import algebra.op.CQuery;
import algebra.op.InformativeObjectQuery;
import app.io.LoadWebFile;
import entities.ConceptAssertion;
import entities.concept.AndConcept;
import entities.concept.Concept;

public class TestEval {

	/**
	 * @param args
	 * @throws SyntaxParsingException 
	 */
	public static void main(String[] args) throws SyntaxParsingException {
		ProjectionDescription parsedPd1 = 
			Parser.createProjectionDescription(Parser.getListOfTokensFromTokenizer(
//					"(pd-and (pd-feature unknwon) (pd-role hasSaleInfo  (pd-and (pd-feature price) (pd-role hasSeller (pd-feature seller-avg-rating)) ) )  )"
//			"(pd-and (pd-feature  user_review) (pd-role hasSaleInfo (pd-feature price)) )"		
					"(pd-and (pd-feature num_user_review) (pd-feature user_review))"
//					"(pd-role R (pd-and (pd-and (pd-feature f) (pd-feature g) )  (pd-concept A) ) )"
//					"(pd-and (pd-feature name) (pd-role supervisedBy (pd-and  (pd-and (pd-feature name) (pd-feature age)) (pd-feature gender)) ) )"
//					"(pd-role supervisedBy (pd-and  (pd-and (pd-feature name) (pd-feature age)) (pd-concept Prof)) )"
			)
					);
		LoadWebFile loader = new LoadWebFile("http://localhost/system/kb/dc2.kbox", false);
//		LoadWebFile loader = new LoadWebFile("http://localhost/system/kb/sample.kbox", false);
		//Concept parsedC = new AndConcept(new PConcept("PhD_Student"), new DomainConceptLT<Role, String>(new Role("age"), "30")); 
//		Concept parsedC1 = new AndConcept(new PConcept("Canon_SX220_HS"), 
//				new ExistsConcept(new Role("hasSaleInfo"), new PConcept("TOP")));
		Concept parsedC1 = Parser.conParser(Parser.getListOfTokensFromTokenizer(
//		"(and Compact_Digital_Camera (forall (role hasManufacturer) (forall (role isLocatedIn) Asia) ) )"		
//		"(and (exists (role hasSaleInfo) top) (not (< (role user_review) 4.00) ) )"	
//				"(not (< (role num_user_review) 00050) )"	//popular cameras, ie. # reviews >= 50
//				"(and (exists (role hasSaleInfo) (not (< (role price) 02000) ) ) (not (< (role user_review) 4.00))  )" //expensive ones and high ratings
//				"A"
				"top"
//				"PhD_Student"
		));
		
		loader.resolveFile();
		DescriptionLogicsKB kb= loader.getKB();
		InformativeObjectQuery pq = new InformativeObjectQuery(parsedPd1, new CQuery(parsedC1));
		QueryVisitorEvaluator eval = new QueryVisitorEvaluator(kb);
		double start = System.currentTimeMillis();
		List<ConceptAssertion> res1 = pq.acceptVisitor(eval);
		System.err.println("time used in sec: "+ (System.currentTimeMillis() - start)/1000);
//		analyzeResults(res1);
		ProjectionDescription parsedPd2 = 
			Parser.createProjectionDescription(Parser.getListOfTokensFromTokenizer(
//					"(pd-and (pd-concept Student) (pd-role supervisedBy (pd-and  (pd-and (pd-feature name) (pd-feature age)) (pd-feature gender)) ) )"
					"(pd-and (pd-feature release_date) (pd-feature user_review))"		
				)
					);
		Concept parsedC2 = Parser.conParser(Parser.getListOfTokensFromTokenizer(
				"top"
				));
		start = System.currentTimeMillis();
		List<ConceptAssertion> res2 = new InformativeObjectQuery(parsedPd2, new CQuery(parsedC2)).acceptVisitor(eval);
		System.err.println("second run, time used in sec: "+ (System.currentTimeMillis() - start)/1000);
		analyzeResults(res2);
	}
	
	private static String[] analyzeResults(List<ConceptAssertion> res){
		String[] rets = new String[res.size()];
		//String ret = "";
		int i=0;
		if(res.size()<1){
			System.err.println("No results returned.");
			return rets;
		}
		for(ConceptAssertion ca:res){
			String ret = "";
			ret += ca.getIndividual().toString() +": \n";
			Concept c = ca.getConcept();
			if(c instanceof AndConcept){
				AndConcept and = (AndConcept) c;
				for(Concept cc:and.getConceptSet()){
//					ret += "\t";
					if(cc instanceof AndConcept){
						for(Concept dd:((AndConcept) cc).getConceptSet()){
							ret +=  dd.toString()+"\n";
						}
					}else
					ret += cc.toString()+" \n";
				}
				ret = ret.substring(0, ret.length()-1);
			}else{
				ret += c.toString();
			}
			rets[i++] = ret;
			System.out.println(ret+"\n");
		}
		return rets;
	}
	
}
